package com.zlyx.easy.core.map;

import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import com.alibaba.fastjson.JSONObject;
import com.zlyx.easy.core.tool.Ops;
import com.zlyx.easy.core.utils.Logger;

public final class Maps {

	/**
	 * 创建空hashMap
	 * 
	 * @return
	 */
	public static <K, V> EasyMap<K, V> newMap() {
		return EasyMap.newMap();
	}

	/**
	 * 创建hashMap
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public static <K, V> EasyMap<K, V> newMap(K key, V value) {
		return EasyMap.newMap(key, value);
	}

	/**
	 * 创建hashMap
	 * 
	 * @param <T>
	 * @param key
	 * @param value
	 * @return
	 */
	public static EasyMap<String, Object> newMap(Object... os) {
		EasyMap<String, Object> map = EasyMap.newMap();
		if (os == null || os.length % 2 != 0) {
			Logger.warn("Wrong length of os:" + os.length);
			return map;
		}
		for (int i = 0; i < os.length; i++) {
			map.put(String.valueOf(os[i]), os[i + 1]);
			i++;
		}
		return map;
	}

	/**
	 * 根据Object对象数组解析创建hashMap
	 * 
	 * @param <T>
	 * @param key
	 * @param value
	 * @return
	 */
	public static EasyMap<Object, Object> fromObjects(Object... os) {
		EasyMap<Object, Object> map = EasyMap.newMap();
		if (os == null || os.length % 2 != 0) {
			Logger.warn("Wrong length of os:" + os.length);
			return map;
		}
		for (int i = 0; i < os.length; i++) {
			map.put(os[i], os[i + 1]);
			i++;
		}
		return map;
	}

	/**
	 * 创建hashMap
	 * 
	 * @param <T>
	 * @param key
	 * @param value
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <V> TableMap<V> newTableMap(String key, V... value) {
		return TableMap.newMap(key, value);
	}

	/**
	 * 解析字符串为map
	 * 
	 * @param kvs
	 * @return
	 */
	public static EasyMap<String, String> parseMap(String input) {
		if (Ops.isNotEmpty(input)) {
			String[] arr = null;
			EasyMap<String, String> map = newMap();
			for (String kv : input.split(";")) {
				if (kv.contains(":")) {
					arr = kv.split(":");
					if (arr.length > 1) {
						map.put(arr[0].trim(), arr[1].trim());
					}
				}
			}
			return map;
		}
		return newMap();
	}

	/**
	 * 转换map
	 * 
	 * @param map
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> EasyMap<String, T> wrapMap(Map<String, Object> map) {
		EasyMap<String, T> tmp = newMap();
		for (String key : map.keySet()) {
			tmp.put(key, (T) map.get(key));
		}
		return tmp;
	}

	/**
	 * 判断参数Map是否为空
	 * 
	 * @param map
	 * @return
	 */
	public static boolean isEmpty(Map<?, ?> map) {
		return map == null || map.size() == 0;
	}

	/**
	 * 判断参数map是否不为空
	 * 
	 * @param map
	 * @return
	 */
	public static boolean isNotEmpty(Map<?, ?> map) {
		return !isEmpty(map);
	}

	/**
	 * map转json
	 * 
	 * @param map
	 * @return
	 */
	public static JSONObject toJson(Map<String, Object> map) {
		return new JSONObject(map);
	}

	/**
	 * 替换map的key
	 * 
	 * @param map    参数map
	 * @param oldKey 旧key
	 * @param newKey 新key
	 * @return
	 */
	public static <K, V> Map<K, V> replaceKey(Map<K, V> map, K oldKey, K newKey) {
		if (map.containsKey(oldKey)) {
			map.put(newKey, map.remove(oldKey));
		}
		return map;
	}

	/**
	 * 移除值为Null或""的key
	 * 
	 * @param map
	 * @return
	 */
	public static <K, V> Map<K, V> removeKeyWithEmptyValue(Map<K, V> map) {
		for (Entry<K, V> entry : map.entrySet()) {
			if (Ops.isEmpty(entry.getValue())) {
				map.remove(entry.getKey());
			}
		}
		return map;
	}

	/**
	 * 解析数组map
	 * 
	 * @param arraysMap
	 * @return
	 */
	public static <V> EasyMap<String, V> parseArrayMap(Map<String, V[]> arraysMap) {
		EasyMap<String, V> params = newMap();
		V[] values = null;
		for (String name : arraysMap.keySet()) {
			values = arraysMap.get(name);
			if (Ops.isNotEmpty(values)) {
				params.put(name, values[0]);
			}
		}
		return params;
	}

	/**
	 * @return
	 */
	public static <K, V> Map<K, V> newConcurrentHashMap() {
		return new ConcurrentHashMap<>();
	}
}
